package com.ternnetwork.wechat.util;

import java.io.FileNotFoundException;
import java.nio.charset.Charset;

import com.csvreader.CsvReader;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.wechat.model.auth.AccessToken;
import com.ternnetwork.wechat.model.tag.AddTagUsersRequest;

public class WechatQyTag {

	
	public static String create_url="https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN";
	
	public static String update_url="https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN";
	
	public static String delete_url="https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&tagid=TAGID";
	
	public static String get_list_url="https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=ACCESS_TOKEN";
 	
	public static String get_user_by_tag_id="https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=TAGID";

	public static String add_tag_users="https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=ACCESS_TOKEN";
	
	public static String del_tag_users="https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=ACCESS_TOKEN";
	
	
	
	
	public static void main(String[] args){
		 
		try {
			CsvReader	r = new CsvReader("/Users/xuwenfeng/Desktop/qyh_dealer_id.csv", ',',Charset.forName("GBK"));
			
			  //读取表头
	        r.readHeaders();
	       
	        //逐条读取记录，直至读完
	        int i=0;
	        while (r.readRecord()) {
	        	i=i+1;
	            //读取一条记录
	            System.out.println(i+" "+r.getRawRecord().replace("\"",""));
	            
	            AddTagUsersRequest t=new AddTagUsersRequest();
	            
	            t.setPartylist(new Long[]{});
	            
	            t.setUserlist(new String[]{r.getRawRecord().replace("\"","")});
	            t.setTagid("2");
	           
	            addTagUsers("wxd0677a981b2a0d3e","1RmXsb46G_mJdUHn7Jiw5_dOW6X4vLQ2ZGt5r6k5AsQxt49JkLiI9syuWrbtt_Qp", JsonUtils.objectToJson(t));
	         
	        }

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      
		
		//list("wxd0677a981b2a0d3e", "1RmXsb46G_mJdUHn7Jiw5_dOW6X4vLQ2ZGt5r6k5AsQxt49JkLiI9syuWrbtt_Qp");
	}
	
	
	public static String create(String corpId,String corpSecret,String json){
	 		AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
	 		String url = create_url.replace("ACCESS_TOKEN", at.getAccess_token());
	 		String respJson = HttpConnectionUtil.httpRequest(url,"POST",json);
	 		return respJson;
	}
	 
	 public static String update(String corpId,String corpSecret,String json){
	 		AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
	 		String url = update_url.replace("ACCESS_TOKEN", at.getAccess_token());
	 		String respJson = HttpConnectionUtil.httpRequest(url,"POST",json);
	 		return respJson;
	}
	 
	 
	 public static String delete(String corpId,String corpSecret,String tagId){
			AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
			String url = delete_url.replace("ACCESS_TOKEN", at.getAccess_token()).replace("TAGID",tagId);
			String respJson = HttpConnectionUtil.httpRequest(url, "POST",null);
			return respJson;
   }
	
    public static String list(String corpId,String corpSecret){
			AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
			String url = get_list_url.replace("ACCESS_TOKEN", at.getAccess_token());
			String respJson = HttpConnectionUtil.httpRequest(url, "POST",null);
			return respJson;
    }
	 
    public static String getUserByTagId(String corpId,String corpSecret,String tagId){
			AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
			String url = get_user_by_tag_id.replace("ACCESS_TOKEN", at.getAccess_token()).replace("TAGID",tagId);
			String respJson = HttpConnectionUtil.httpRequest(url, "POST",null);
			return respJson;
    }
    
    public static String addTagUsers(String corpId,String corpSecret,String json){
		AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
		String url = add_tag_users.replace("ACCESS_TOKEN", at.getAccess_token());
		String respJson = HttpConnectionUtil.httpRequest(url,"POST",json);
		return respJson;
    }
    
    public static String delTagUsers(String corpId,String corpSecret,String json){
 		AccessToken at = AccessTokenUtil.getQyAccessToken(corpId, corpSecret);
 		String url = del_tag_users.replace("ACCESS_TOKEN", at.getAccess_token());
 		String respJson = HttpConnectionUtil.httpRequest(url,"POST",json);
 		return respJson;
     }
}
